package com.cskaoyan.service.impl;

import com.cskaoyan.bean.MarketCoupon;
import com.cskaoyan.bean.leihao.vo.*;
import com.cskaoyan.mapper.*;
import com.cskaoyan.service.HomeIndexService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.ArrayList;
import java.util.List;

/**
 * @Auther: leihao
 * @Date: Created in 11:20 2022/6/29
 */
@Service
@Transactional
public class HomeIndexServiceImpl implements HomeIndexService {
    @Autowired
    MarketAdMapper marketAdMapper;
    @Autowired
    MarketBrandMapper brandMapper;
    @Autowired
    MarketCategoryMapper categoryMapper;
    @Autowired
    MarketCouponMapper couponMapper;
    @Autowired
    MarketGoodsMapper goodsMapper;
    @Autowired
    MarketTopicMapper topicMapper;
    @Override
    public WxHomeIndexVo getHomeIndex() {
        List<BannerDTO> banner = marketAdMapper.selectWithoutStartEndTime();
        List<BrandListDTO> brandList = brandMapper.selectHomeIdex();
        List<ChannelDTO> channel = categoryMapper.selectCategoryByPidIsZero();
        List<CouponListDTO> couponList = couponMapper.selectHomeIndex();

        ArrayList<FloorGoodsListDTO> floorGoodsList = new ArrayList<>();
        for (int i = 0; i < channel.size(); i++) {
            Integer pid = channel.get(i).getId();
            List<Integer> categoryList = categoryMapper.selectIdByPid(pid);
            List<HotGoodsListDTO> goodsList = null;
            if(categoryList.size()>0){
                goodsList = goodsMapper.selectByCategoryList(categoryList);
            }

            FloorGoodsListDTO floorGoods = new FloorGoodsListDTO();
            floorGoods.setGoodsList(goodsList);
            floorGoods.setId(channel.get(i).getId());
            floorGoods.setName(channel.get(i).getName());
            floorGoodsList.add(floorGoods);
        }

        List<HotGoodsListDTO> hotGoodsList = goodsMapper.selectByIsHot();
        List<NewGoodsListDTO> newGoodsList = goodsMapper.selectByIsNew();
        List<TopicListDTO> topicList = topicMapper.selectHomeIndex();

        WxHomeIndexVo homeIndexVo = new WxHomeIndexVo();
        homeIndexVo.setBanner(banner);
        homeIndexVo.setBrandList(brandList);
        homeIndexVo.setChannel(channel);
        homeIndexVo.setCouponList(couponList);
        homeIndexVo.setFloorGoodsList(floorGoodsList);
        homeIndexVo.setHotGoodsList(hotGoodsList);
        homeIndexVo.setNewGoodsList(newGoodsList);
        homeIndexVo.setTopicList(topicList);
        return homeIndexVo;
    }
}
